package com.lzb.randomturntable.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.lzb.randomturntable.BaseAc;
import com.lzb.randomturntable.Content;
import com.lzb.randomturntable.R;
import com.lzb.randomturntable.adapter.OptionAdapter;
import com.lzb.randomturntable.bean.TurntableBean;
import com.lzb.randomturntable.databinding.ActivityAddTurntableBinding;
import com.lzb.randomturntable.utils.AppSpUtils;

import java.util.ArrayList;
import java.util.List;

public class AddTurntableActivity extends BaseAc<ActivityAddTurntableBinding> implements View.OnClickListener, OptionAdapter.OnOptionChangeListener, OnItemChildClickListener {

    public static int selPosition;
    private TurntableBean turntableBean;
    private OptionAdapter optionAdapter;
    private boolean isCopy;

    @Override
    protected int onCreate() {
        return R.layout.activity_add_turntable;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAddTurntableBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);
        mDataBinding.llAddOption.setOnClickListener(this);

        mDataBinding.rvOptionList.setLayoutManager(new LinearLayoutManager(mContext));
        optionAdapter = new OptionAdapter();
        optionAdapter.addChildClickViewIds(R.id.ivOptionItemDelete);
        optionAdapter.setOnOptionChangeListener(this);
        optionAdapter.setOnItemChildClickListener(this);
        mDataBinding.rvOptionList.setAdapter(optionAdapter);

        Intent intent = getIntent();
        isCopy = intent.getBooleanExtra(Content.isCopy, false);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.llAddOption) {//添加选项
            // 添加新的空选项
            turntableBean.getOptionList().add("");
            optionAdapter.setList(turntableBean.getOptionList());
        } else if (id == R.id.tvSave) {//保存
            String problem = mDataBinding.etProblem.getText().toString();
            if (TextUtils.isEmpty(problem)) {
                ToastUtils.showShort(getString(R.string.please_input_problem));
                return;
            }

            // 检查非空选项数量
            List<String> optionList = turntableBean.getOptionList();
            int nonEmptyCount = 0;
            for (String option : optionList) {
                if (!TextUtils.isEmpty(option.trim())) {
                    nonEmptyCount++;
                }
            }

            if (nonEmptyCount < 2) {
                ToastUtils.showShort(getString(R.string.please_add_least_two_option));
                return;
            }

            // 清理空选项并保存
            List<String> cleanedList = new ArrayList<>();
            for (String option : optionList) {
                if (!TextUtils.isEmpty(option.trim())) {
                    cleanedList.add(option.trim());
                }
            }

            // 更新turntableBean的选项列表
            turntableBean.setOptionList(cleanedList);
            turntableBean.setProblem(problem.trim());

            // 保存到SharedPreferences
            saveTurntable();
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        int id = view.getId();
        if (id == R.id.ivOptionItemDelete) {
            turntableBean.getOptionList().remove(position);
            optionAdapter.setList(turntableBean.getOptionList());
        }
    }

    @Override
    protected void initData() {
        if (selPosition == -1) {
            List<String> list = new ArrayList<>();
            list.add("");
            list.add("");
            turntableBean = new TurntableBean("", list);
        } else if (selPosition == -2) {
            List<String> list = new ArrayList<>();
            list.add("减脂餐");
            list.add("广东菜");
            list.add("重庆火锅");
            list.add("麻辣烫");
            list.add("炸鸡");
            list.add("烧烤");
            turntableBean = new TurntableBean("今晚吃什么", list);
        } else {
            turntableBean = AppSpUtils.getTurntableList().get(selPosition);
            if (isCopy) {
                mDataBinding.tvTitle.setText(getString(R.string.copy_turntable));
            } else {
                mDataBinding.tvTitle.setText(getString(R.string.edit_turntable));
            }
        }
        mDataBinding.etProblem.setText(turntableBean.getProblem());
        optionAdapter.setList(turntableBean.getOptionList());
    }

    @Override
    public void onOptionChanged(int position, String newText) {
        // 更新turntableBean中的optionList
        if (turntableBean != null && turntableBean.getOptionList() != null) {
            turntableBean.getOptionList().set(position, newText);
        }
    }

    private void saveTurntable() {
        List<TurntableBean> turntableList = AppSpUtils.getTurntableList();
        if (turntableList == null) {
            turntableList = new ArrayList<>();
        }

        if (selPosition == -1 || selPosition == -2 || isCopy) {
            // 新建转盘
            turntableList.add(turntableBean);
        } else {
            // 编辑现有转盘
            turntableList.set(selPosition, turntableBean);
        }

        AppSpUtils.saveTurntableList(turntableList);
        ToastUtils.showShort(getString(R.string.save_suc));
        finish();
    }
}